package org.hy.controller.mypage;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.hy.domain.User;
import org.hy.domain.mypage.MyCareerShow;
import org.hy.domain.mypage.MyCareerShowContent;
import org.hy.service.mypage.IMyCareerShowService;
import org.hy.util.AppConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;


/**
 * 职业秀控制器
 * @author Tom.Feng
 *
 */
@Controller
@RequestMapping("/mycareer/show/")
public class MyCareerShowController {
	
	@Autowired
	private IMyCareerShowService myCareerShowService;
	
	@RequestMapping("list/{page}")
	public String findList(@PathVariable("uid") int uid,@PathVariable("page") int page,@PathVariable("size") int size,Model model){
		List<MyCareerShow> showList = (List<MyCareerShow>) myCareerShowService.findMyCareerShowWithDefaultCommentsByUid(uid, page, size);
		int count = 1;
		model.addAttribute("showList", showList);
		model.addAttribute("count", count);
		return "mycareer/show/list";
	}
	
	@RequestMapping("list")
	public String findList1(Model model,HttpSession session){
		User thisUser = (User)session.getAttribute("user");
		int uid = thisUser.getId();
		return findList(uid,1,10,model);
	}
	
	@RequestMapping("new")
	public String add(Map<String,Object> model){
		model.put("edit", false);
		model.put("title","添加职业秀");
		model.put("action","mycareer/show/create" );
		model.put("myCareerShow",new MyCareerShow());
		return "mycareer/show/form";
	}
	
	@RequestMapping("edit/{id}")
	public String edit(@PathVariable int id,Map<String,Object> model){
		MyCareerShow myCareerShow = myCareerShowService.findMyCareerShowById(id);
		model.put("myCareerShow",myCareerShow);
		model.put("edit", true);
		model.put("title", "编辑职业秀");
		model.put("action", "mycareer/show/update");
		return "mycareer/show/form";
	}
	
	/**
	 * 添加职业秀
	 * @param myCareerShow
	 * @param myCareerShowContent
	 * @param session
	 * @return
	 */
	@RequestMapping("create")
	public String create(MyCareerShow myCareerShow,MyCareerShowContent myCareerShowContent,HttpSession session){
		myCareerShow.setIntime(AppConstant.now()); 
		myCareerShowService.addMyCareerShow(myCareerShow,myCareerShowContent);
		//myCareerShowService.addMyCareerShowContent(myCareerShowContent);
		return "redirect:list";
	}
	
	/**
	 * 编辑职业秀
	 * @param id
	 * @param myCareerShowNew
	 * @return
	 */
	@RequestMapping("update/{id}")
	public String update(@PathVariable int id,MyCareerShow myCareerShowNew){
		MyCareerShow myCareerShowOld = myCareerShowService.findMyCareerShowById(id);
		myCareerShowOld.setContent(myCareerShowNew.getContent());
		myCareerShowOld.setTitle(myCareerShowNew.getTitle());
		return "redirect:list";
	}
	/**
	 * 删除职业秀
	 * @param id
	 * @param session
	 * @return
	 */
	@RequestMapping("delete/{id}")
	public String delete(@PathVariable int id,HttpSession session){
		User loginedUser = (User)session.getAttribute("user");
		int OwnerId = loginedUser.getId();
		myCareerShowService.deleteMyCareerShow(id, OwnerId);
		return "redirect:list";
	}
	
}
